<?php

namespace App\Http\Controllers\Customer;

use App\Helpers\Code;
use App\Models\Customer\FamilyMember;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class FamilyMemberController extends Controller
{

    /**
     * FamilyMemberController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new FamilyMember();

        $this->_validators();
    }

    /**
     * 添加家系成员
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addFamilyMember(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addFamilyMember($data);
            $this->checked($model, [
                'null' => 'customer.family_members.relation_not_exists',
                Code::INSIDE_ERROR => 'customer.family_members.is_patient_not_exists',
                Code::INSIDE_ERROR_1 => 'customer.family_members.customer_department_doctor_not_exists',
                Code::INSIDE_ERROR_2 => 'customer.family_members.family_x_already_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑家系成员
     * @param Request $request
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function editFamilyMember(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['code', 'created_by', 'data_scope']));
            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    case 'clinical_diagnosis':
                    case 'family_history':
                        break;
                    default:
                        $rules[$key] = $this->rules[$key];
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editFamilyMember($data, $id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'customer.family_members.relation_not_exists',
                Code::INSIDE_ERROR_1 => 'customer.family_members.is_patient_not_exists',
                Code::INSIDE_ERROR_2 => 'customer.family_members.customer_department_doctor_not_exists',
                Code::INSIDE_ERROR_3 => 'customer.family_members.family_x_already_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除家系成员
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function deleteFamilyMember($id)
    {
        try {
            $model = $this->model->deleteFamilyMember($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取家系成员列表
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getFamilyMemberList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'desc';

            return preprocess($this->model->getFamilyMemberList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'family_id' => 'bail|required|numeric|exists:families,id',
            'title' => 'bail|required|max:100',
            'title_amend' => 'bail|required|max:100',
            'gender' => 'bail|required|in:0,1,2',
            'birthday' => 'bail|nullable|date',
            'age' => 'bail|nullable|max:5',
            'age_unit' => 'bail|nullable|max:100',
            'age_range' => 'bail|nullable|max:100',
            'age_range_unit' => 'bail|nullable|max:50',
            'relation' => 'bail|required|max:200',
            'birthplace' => 'bail|nullable|max:100',
            'address' => 'bail|nullable|max:100',
            'wechat' => 'bail|nullable|max:50',
            'contact' => 'bail|nullable|max:50',
            'contact_phone' => 'bail|nullable|max:20',
            'nation' => 'bail|nullable|max:20',
            'is_patient' => 'bail|required|max:200',
            'case_code' => 'bail|nullable|max:200',
            'clinical_keyword' => 'bail|nullable|max:200',
            'first_date' => 'bail|nullable|date',
            'identity_card' => 'bail|nullable|max:25',
            'customer' => 'bail|nullable|numeric',
            'department' => 'bail|nullable|numeric',
            'doctor' => 'bail|nullable|numeric',
        ];

        $this->messages = [
            'family_id.required' => trans('customer.family_members.family_id_required'),
            'family_id.numeric' => trans('customer.family_members.family_id_numeric'),
            'family_id.exists' => trans('customer.family_members.family_id_exists'),
            'title.required' => trans('customer.family_members.title_required'),
            'title.max' => trans('customer.family_members.title_max'),
            'title_amend.required' => trans('customer.family_members.title_amend_required'),
            'title_amend.max' => trans('customer.family_members.title_amend_max'),
            'gender.required' => trans('customer.family_members.gender_required'),
            'gender.in' => trans('customer.family_members.gender_in'),
            'birthday.date' => trans('customer.family_members.birthday_date'),
            'age.max' => trans('customer.family_members.age_max'),
            'age_unit.max' => trans('customer.family_members.age_unit_max'),
            'age_range.max' => trans('customer.family_members.age_range_max'),
            'age_range_unit.max' => trans('customer.family_members.age_range_unit_max'),
            'relation.required' => trans('customer.family_members.relation_required'),
            'relation.max' => trans('customer.family_members.relation_max'),
            'birthplace.max' => trans('customer.family_members.birthplace_max'),
            'address.max' => trans('customer.family_members.address_max'),
            'wechat.max' => trans('customer.family_members.wechat_max'),
            'contact.max' => trans('customer.family_members.contact_max'),
            'contact_phone.max' => trans('customer.family_members.contact_phone_max'),
            'nation.max' => trans('customer.family_members.nation_max'),
            'is_patient.required' => trans('customer.family_members.is_patient_required'),
            'is_patient.max' => trans('customer.family_members.is_patient_max'),
            'case_code.max' => trans('customer.family_members.case_code_max'),
            'clinical_keyword.max' => trans('customer.family_members.clinical_keyword_max'),
            'first_date.date' => trans('customer.family_members.first_date_date'),
            'identity_card.max' => trans('customer.family_members.identity_card_max'),
            'customer.numeric' => trans('customer.family_members.customer_numeric'),
            'department.numeric' => trans('customer.family_members.department_numeric'),
            'doctor.numeric' => trans('customer.family_members.doctor_numeric'),
        ];
    }
}
